from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration


def generate_launch_description():
    """
    ros2 launch cpp01_launch py04_args_launch.py back_r:=150
    ros2 launch cpp01_launch py04_args_launch.py back_r:=150 back_g:=0 back_g:=0
 
    """
    bg_r=DeclareLaunchArgument(name="back_r",default_value='255')
    bg_g=DeclareLaunchArgument(name="back_g",default_value="255")
    bg_b=DeclareLaunchArgument(name="back_b",default_value="255")
    t1=Node(package="turtlesim",
            executable="turtlesim_node",
            parameters=[{"background_r":LaunchConfiguration("back_r"),
                         "background_g":LaunchConfiguration("back_g"),
                         "background_b":LaunchConfiguration("back_b")
                         }]
    )
    return LaunchDescription([bg_r,bg_g,bg_b,t1])

"""
def generate_launch_description():
    # 1. 声明动态参数
    bg_r_arg = DeclareLaunchArgument(
        name="back_r",
        default_value='255',  # 默认值
        description="控制 turtlesim 背景颜色的红色分量，此动态参数会覆盖yaml中的配置"
    )
    bg_r_config = LaunchConfiguration('back_r')
    
    # 2. 获取YAML配置文件的路径（假设文件存在）
    package_name = 'cpp01_launch'
    package_share_dir = get_package_share_directory(package_name)
    param_file_path = os.path.join(package_share_dir, 'config', 'haha.yaml')
    
    # 3. 关键：在parameters列表中，先加载YAML文件，再传入动态参数字典。
    # 这样，动态参数就会覆盖YAML文件中的同名参数。
    t2 = Node(
        package="turtlesim",
        executable="turtlesim_node",
        name="haha",
        parameters=[
            param_file_path,  # 第一优先级：静态YAML配置
            {"background_r": bg_r_config}  # 第二优先级（更高）：动态参数，会覆盖上面的background_r
        ],
        respawn=True
    )
    
    return LaunchDescription([
        bg_r_arg,
        t2
    ])
"""